package com.redsoil.house.ui.mine.fragment;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.EmptyUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnClickListener;
import com.orhanobut.dialogplus.ViewHolder;
import com.redsoil.house.R;
import com.redsoil.house.app.AppContext;
import com.redsoil.house.base.RootFragment;
import com.redsoil.house.base.RxBus;
import com.redsoil.house.model.event.LogoutEvent;
import com.redsoil.house.ui.capital.contract.CapitalContract;
import com.redsoil.house.ui.capital.presenter.CapitalPresenter;
import com.redsoil.house.ui.login.activity.ChangeEmailActivity;
import com.redsoil.house.ui.login.activity.ChangePwdActivity;
import com.redsoil.house.ui.login.activity.LawActivity;
import com.redsoil.house.ui.mine.activity.AboutUsActivity;
import com.redsoil.house.ui.mine.contract.MineInfoContract;
import com.redsoil.house.ui.mine.presenter.MineInfoPresenter;
import com.redsoil.house.ui.news.activity.NewsInfoActivity;
import com.redsoil.house.widget.PropertyFileItem;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 资产主界面
 */
public class MineInfoFragment extends RootFragment<MineInfoPresenter> implements MineInfoContract.View {

    @BindView(R.id.user_head_img)
    ImageView user_head_img;

    @BindView(R.id.user_name_tv)
    TextView user_name_tv;

    @BindView(R.id.change_password)
    PropertyFileItem change_password;

    @BindView(R.id.change_email)
    PropertyFileItem change_email;

    @BindView(R.id.about_us)
    PropertyFileItem about_us;

    @BindView(R.id.terms_use)
    PropertyFileItem terms_use;

    @BindView(R.id.personal_privacy)
    PropertyFileItem personal_privacy;

    @BindView(R.id.logout_button)
    Button logout_button;

    private DialogPlus logoutDialog;

    public static MineInfoFragment newInstance() {
        Bundle args = new Bundle();

        MineInfoFragment fragment = new MineInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initEventAndData() {

        user_name_tv.setText(AppContext.getInstance().getUsername());

        if(AppContext.getInstance().getSex() != null && AppContext.getInstance().getSex().equals("1")){
            user_head_img.setImageResource(R.drawable.icon_mine_head_boy);
        }else {
            user_head_img.setImageResource(R.drawable.icon_mine_head_girl);
        }

        change_password.setData(R.drawable.icon_change_password,getResources().getString(R.string.change_password),"",getResources().getDrawable(R.drawable.shape_top_edge));
        change_email.setData(R.drawable.icon_change_email,getResources().getString(R.string.change_email),"",getResources().getDrawable(R.drawable.shape_top_edge));
        about_us.setData(R.drawable.icon_about_us,getResources().getString(R.string.about_us),"",getResources().getDrawable(R.drawable.shape_top_edge));
        terms_use.setData(R.drawable.icon_terms_use,getResources().getString(R.string.terms_use),"",getResources().getDrawable(R.drawable.shape_top_edge));
        personal_privacy.setData(R.drawable.icon_personal_privacy,getResources().getString(R.string.personal_privacy),"",getResources().getDrawable(R.drawable.shape_top_bottom_edge));
    }

    @OnClick(R.id.change_password)
    void onChangePassword(){
        getContext().startActivity(ChangePwdActivity.newInstance(getContext()));
    }

    @OnClick(R.id.change_email)
    void onChangeEmail(){
        getContext().startActivity(ChangeEmailActivity.newInstance(getContext()));
    }

    @OnClick(R.id.about_us)
    void onAboutUs(){
        getContext().startActivity(AboutUsActivity.newInstance(getContext()));
    }

    @OnClick(R.id.terms_use)
    void onTermUse(){
        getContext().startActivity(LawActivity.newInstance(getContext(),true));
    }

    @OnClick(R.id.personal_privacy)
    void onPersonalPrivacy(){
        getContext().startActivity(LawActivity.newInstance(getContext(),false));
    }

    @OnClick(R.id.logout_button)
    void onLogout(){

        if (EmptyUtils.isEmpty(logoutDialog)) {
            logoutDialog = DialogPlus.newDialog(getContext())
                    .setGravity(Gravity.CENTER)
                    .setContentHolder(new ViewHolder(R.layout.dialog_logout))
                    .setContentBackgroundResource(R.drawable.dialog_write_corner_bg)
                    .setCancelable(true)//设置不可取消   可以取消
                    .setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(DialogPlus dialog, View view) {
                            switch (view.getId()) {
                                case R.id.dialog_logout_btn_cancel:
                                    dialog.dismiss();
                                    break;
                                case R.id.dialog_logout_btn_confirm:

                                    mPresenter.logout();

                                    dialog.dismiss();
                                    break;
                            }
                        }
                    }).create();
        }
        logoutDialog.show();

    }

    @Override
    public void responeError(String errorMsg) {
        ToastUtils.showShortToast("退出失败，请重试");
    }

    @Override
    public void logoutSuccess() {
        ToastUtils.showShortToast("退出成功");
        //需要发送事件
        RxBus.getDefault().post(new LogoutEvent(LogoutEvent.LOGOUT));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine_info;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }
}
